﻿#pragma once
#include<iostream>
#include<assert.h>
#include<vector>
using namespace std;

template<class K, class V>
struct AVLTreeNode
{
	// 需要parent指针，后续更新平衡因子可以看到 
	pair<K, V> _kv;
	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right;
	AVLTreeNode<K, V>* _parent;
	int _bf; // balance factor 平衡因子

	AVLTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _bf(0)
	{}
};

template<class k, class v>
class AVLTree
{

public:
	//typedef AVLTreeNode<k, v> Node;
	using Node = AVLTreeNode<k, v>;

	bool Insert(const pair<k,v>& kv)
	{
		Node* cur = _root;
		Node* parent = cur;

		if (cur == nullptr)
		{
			_root = new Node(kv);
			return true;
		}
		
		while(cur)
		{
			if (cur->_kv.first > kv.first)       //pair重载了大小于运算符
			{
				parent = cur;
				cur = cur->_left;
				
			}
			else if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
					
			}
			else
				return false;
		}
		if (parent->_kv.first > kv.first)
		{
			cur = new Node(kv);
			parent->_left = cur;
		}
		else
		{
			cur = new Node(kv);
			parent->_right = cur;
		}
		
		//连接到父亲节点
		cur->_parent = parent;


		//控制平衡
		//更新平衡因子
		while (parent)
		{
			if (parent->_left == cur)
				parent->_bf--;
			else
				parent->_bf++;

			if (parent->_bf == 0)
			{
				break;
			}
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				//进行旋转
		
				if (parent->_bf == -2 && cur->_bf == -1)//右单旋（单纯的左边高）
				{
					RotateR(parent);
				}
				else if(parent->_bf==2&&cur->_bf==1)//左单选（单纯的右边高）
				{
					RotateL(parent);
				}
				else if (parent->_bf==-2&&cur->_bf==1)//左右双旋:先左单旋再右单旋（不单纯的左边高）
				{
					RotateLR(parent);
				}
				else if (parent->_bf==2&&cur->_bf==-1)//右左双旋：先右单旋再左单旋（不单纯的右边高）
				{
					RotateRL(parent);
				}
				else
				{
					assert(false);
				}

				break;
			}
			else
			{
				assert(false);
				//预防未知错误的编程逻辑
			}
		}


		return true;
	}

	//右单旋
	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		Node* pParent = parent->_parent;
		subL->_right = parent;
		parent->_parent = subL;

		parent->_left = subLR;
		if(subLR)
			subLR->_parent = parent;

		//判断：若旋转的节点为根，则代表这是一颗完整的树，不需要处理
		//若旋转的节点不为根，则代表这是一颗子树，需要连接至上面的树
		if (pParent == nullptr)
		{
			_root=subL;
			subL->_parent = nullptr;
			
		}
		else
		{
			if (pParent->_left == parent)
			{
				pParent->_left = subL;
				subL->_parent = pParent;
			}
			else
			{
				pParent->_right = subL;
				subL->_parent = pParent;
			}
		}

		//更新平衡因子
		subL->_bf = parent->_bf = 0;

	}

	//左单旋
	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		
		parent->_right = subRL;
		if(subRL)
			subRL->_parent = parent;

		Node* pParent = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;

		if (pParent == nullptr)
		{
			_root = subR;
			subR->_parent= nullptr;
		}
		else
		{
			if (pParent->_left == parent)
			{
				pParent->_left = subR;
			}
			else
			{
				pParent->_right = subR;
			}
			subR->_parent = pParent;
		}

		//更新平衡因子
		subR->_bf = parent->_bf = 0;

	}

	//左右双旋
	void RotateLR(Node* parent)
	{
		//提前记录，插入的位置
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;

		//直接复用左右单旋
		RotateL(subL);
		RotateR(parent);

		if (bf == 0)
		{
			parent->_bf = 0;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = 0;
			subL->_bf = -1;
			subLR->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 1;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}


	//右左双旋
	void RotateRL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf;

		//直接复用右左单旋
		RotateR(subR);
		RotateL(parent);

		if (bf == 0)
		{
			parent->_bf = 0;
			subR->_bf = 0;
			subRL->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = -1;
			subR->_bf = 0;
			subRL->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 0;
			subR->_bf = 1;
			subRL->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}

	void InOrder()
	{
		_InOrder(_root);
	}


	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}
	bool IsBalanceTree(Node* root)
	{
		// 空树也是AVL树 
		if (nullptr == root)
			return true;
		// 计算pRoot结点的平衡因⼦：即pRoot左右⼦树的⾼度差 
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		int diff = rightHeight - leftHeight;
	}

	Node* GetRoot()
	{
		return _root;
	}

private:

	void _InOrder(Node* root)
	{
		if (!root)
			return;

		_InOrder(root->_left);
		cout << root->_kv.first << " " << root->_kv.second << endl;
		_InOrder(root->_right);
	}


	Node* _root = nullptr;
};